<template>
	<view>
		<view v-if="isLogin">
			<view class="head-container">
				<image :src="avatar" mode="aspectFill" class="img-avatar">
				</image>
				<view class="user-info-container">
					<view class="nick-type-container">
						<text class="nick-badge">{{nick + "[" + userTypes[type] + "]"}}</text>
					</view>
				</view>
			</view>
			<text class="iconfont icon-ziyuanxhdpi icon-zhuxiao" @click="clickLogout"></text>
		</view>
		<view class="manage">
			<view v-if="type == 0 || type == 1 || type ==2 " class="manage-function">
				<text class="iconfont icon-gerenzhongxin-gerenxinxi icon"></text>
				<text class="title-name">个人中心</text>
			</view>
			<view v-if="type == 0" class="manage-function">
				<text class="iconfont icon-yonghuguanli-yonghuguanli icon" @click="usermanage"></text>
				<text class="title-name">用户管理</text>
			</view>
			<view v-if="type == 0" class="manage-function">
				<text class="iconfont icon-kaifazhequanxianpeizhi icon" @click="permissionmanage"></text>
				<text class="title-name">权限管理</text>
			</view>
			<view v-if="type == 1" class="manage-function">
				<text class="iconfont icon-kechengguanli icon" @click="coursemanage"></text>
				<text class="title-name">课程管理</text>
			</view>
			<view v-if="type == 1" class="manage-function">
				<text class="iconfont icon-xueshengguanli icon" @click="studentmanage"></text>
				<text class="title-name">学生管理</text>
			</view>
			<view v-if="type == 1" class="manage-function">
				<text class="iconfont icon-tianjiakecheng icon" @click="addcourse"></text>
				<text class="title-name">添加课程</text>
			</view>
			<view v-if="type == 2" class="manage-function">
				<text class="iconfont icon-kechengbiao icon" @click="courselist"></text>
				<text class="title-name">课程列表</text>
			</view>
		</view>
		<login-dialog ref="login" @after="afterLogin"></login-dialog>
	</view>
</template>

<script>
	const app = getApp()
	export default {
		data() {
			return {
				isLogin: false,
				avatar: '',
				name: '',
				email: '',
				nick: '',
				type: app.globalData.type,
				userTypes: ["管理员", "教师", "学生"]
			}
		},
		mounted() {
			if (!this.isLogin) {
				this.$refs.login.show()
			}
		},
		methods: {
			afterLogin(logout) {
				if (logout) {
					this.$refs.login.show()
					return
				}
				if (this.isLogin) {
					return
				}
				this.isLogin = app.globalData.token.length > 0
				this.avatar = this.$params.host + app.globalData.avatar
				this.type = app.globalData.type
				this.name = app.globalData.userName
				this.email = app.globalData.email
				this.nick = app.globalData.nickName
			},
			clickLogout() {
				this.isLogin = false
				app.globalData.avatar = '/static/default_user.png'
				app.globalData.uid = ''
				app.globalData.userName = ''
				app.globalData.nickName = ''
				app.globalData.type = -1
				app.globalData.email = ''
				app.globalData.token = ''
				app.globalData.phone = ''
				app.globalData.apply = ''
				this.$noti.post(this.$params.noti_login_status, true)
			},
			usermanage() {
				uni.navigateTo({
					url: '../admin/usermanage/usermanage',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			permissionmanage() {
				uni.navigateTo({
					url: '../admin/permissionmanage/permissionmanage',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			coursemanage() {
				uni.navigateTo({
					url: '../teacher/coursemanage/coursemanage',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			studentmanage() {
				uni.navigateTo({
					url: '../teacher/studentmanage/studentmanage',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			addcourse() {
				uni.navigateTo({
					url: '../teacher/addcourse/addcourse',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			courselist() {
				uni.navigateTo({
					url: '../student/courselist/courselist',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			}
		}
	}
</script>

<style>
	@import url("index.css");
</style>
